rm(list = ls())

setwd('/path/to/replication/')

library(data.table)
library(zoo)
library(ggplot2)

# Figure C1: Total number of hate crimes during the lockdown periods
crimes <- fread('./data/2007_2020_hate_crimes_classified.csv')
crimes[, month:= month(as.POSIXlt(as.Date(Date, format= "%d-%m-%Y"), format="%d-%m-%Y"))]
crimes[, year:= year(as.POSIXlt(as.Date(Date, format= "%d-%m-%Y"), format="%d-%m-%Y"))]

## Figure C1 (a)
ggplot(crimes[month>=4 & month<=6, .(num=.N), by=year], aes(x=year, y=num)) +
  geom_bar(stat="identity") +
  scale_x_continuous(breaks = seq(2007,2020,1)) +
  xlab(NULL) + ylab('number of crimes Apr-Jun') +
  theme(
    legend.position="none",
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(), axis.line = element_line(colour = "black"
    ),axis.title.y = element_text(size = rel(1.2)), axis.text.x=element_text(size=12, angle = 90, vjust = 0.5, hjust=1),
    axis.text.y=element_text(size=12))
ggsave('./crimes_apr_jun.pdf', width=6, height=4.5)

## Figure C1 (b)
ggplot(crimes[month>=7 & month<=10, .(num=.N), by=year], aes(x=year, y=num)) +
  geom_bar(stat="identity") +
  scale_x_continuous(breaks = seq(2007,2020,1)) +
  xlab(NULL) + ylab('number of crimes Jul-Oct') +
  theme(
    legend.position="none",
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(), axis.line = element_line(colour = "black"
    ),axis.title.y = element_text(size = rel(1.2)), axis.text.x=element_text(size=12, angle = 90, vjust = 0.5, hjust=1),
    axis.text.y=element_text(size=12))
ggsave('./crimes_jul_oct.pdf', width=6, height=4.5)

## Figure C1 (c)
ggplot(crimes[month>=11 & month<=12, .(num=.N), by=year], aes(x=year, y=num)) +
  geom_bar(stat="identity") +
  scale_x_continuous(breaks = seq(2007,2020,1)) +
  xlab(NULL) + ylab('number of crimes Nov-Dec') +
  theme(
    legend.position="none",
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(), axis.line = element_line(colour = "black"
    ),axis.title.y = element_text(size = rel(1.2)), axis.text.x=element_text(size=12, angle = 90, vjust = 0.5, hjust=1),
    axis.text.y=element_text(size=12))
ggsave('./crimes_nov_dec.pdf', width=6, height=4.5)


# Figure C2: Share of Asian hate crimes during the lockdown periods
data_plot <- merge(crimes[month>=4 & month<=6, .(total=.N), by=year], crimes[month>=4 & month<=6 & region_broad=='Asia', .(asian=.N), by=year],
                   by='year')
data_plot[, share:=asian/total]

ggplot(data_plot, aes(x=year, y=share)) +
  geom_bar(stat="identity") +
  scale_x_continuous(breaks = seq(2007,2020,1)) +
  xlab(NULL) + ylab('share of Asian crimes Apr-Jun') +
  theme(
    legend.position="none",
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(), axis.line = element_line(colour = "black"
    ),axis.title.y = element_text(size = rel(1.2)), axis.text.x=element_text(size=12, angle = 90, vjust = 0.5, hjust=1),
    axis.text.y=element_text(size=12))
ggsave('./share_asian_crimes_apr_jun.pdf', width=6, height=4.5)


data_plot <- merge(crimes[month>=7 & month<=10, .(total=.N), by=year], crimes[month>=7 & month<=10 & region_broad=='Asia', .(asian=.N), by=year],
                   by='year')
data_plot[, share:=asian/total]
ggplot(data_plot, aes(x=year, y=share)) +
  geom_bar(stat="identity") +
  scale_x_continuous(breaks = seq(2007,2020,1)) +
  xlab(NULL) + ylab('share of Asian crimes Jul-Oct') +
  theme(
    legend.position="none",
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(), axis.line = element_line(colour = "black"
    ),axis.title.y = element_text(size = rel(1.2)), axis.text.x=element_text(size=12, angle = 90, vjust = 0.5, hjust=1),
    axis.text.y=element_text(size=12))
ggsave('./share_asian_crimes_jul_oct.pdf', width=6, height=4.5)

data_plot <- merge(crimes[month>=11 & month<=12, .(total=.N), by=year], crimes[month>=11 & month<=12 & region_broad=='Asia', .(asian=.N), by=year],
                   by='year')
data_plot[, share:=asian/total]
ggplot(data_plot, aes(x=year, y=share)) +
  geom_bar(stat="identity") +
  scale_x_continuous(breaks = seq(2007,2020,1)) +
  xlab(NULL) + ylab('share of Asian crimes Nov-Dec') +
  theme(
    legend.position="none",
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    panel.background = element_blank(), axis.line = element_line(colour = "black"
    ),axis.title.y = element_text(size = rel(1.2)), axis.text.x=element_text(size=12, angle = 90, vjust = 0.5, hjust=1),
    axis.text.y=element_text(size=12))
ggsave('./share_asian_crimes_nov_dec.pdf', width=6, height=4.5)
